/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import Controlador.Bean.Produccion.Maquina;
import Controlador.Bean.Produccion.TipoMaquina;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author César
 */
public class SeleccionarMaquinas extends javax.swing.JInternalFrame {

    /**
     * Creates new form SeleccionarMaquinas
     */

    private int numHorneado;
    private int numDecorado;
    private int numRellenado;
    private int numEmpaquetado;
    private ArrayList<Maquina> maquinas = new ArrayList();
    asignacionHM ventanaAsignacionHM;
    SeleccionarMaquinas(ArrayList<Maquina> maquinasHabilidatas,ArrayList<Maquina> MaquinasSeleccionables,asignacionHM aThis) {
        
        initComponents();
        ventanaAsignacionHM = aThis;
        initComp(maquinasHabilidatas,MaquinasSeleccionables);
        this.maquinas = maquinasHabilidatas;
    }


    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGuardarMaquinas = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        NumMaquinasHorneado = new javax.swing.JSpinner();
        NumMaquinasDecorado = new javax.swing.JSpinner();
        NumMaquinasRellenado = new javax.swing.JSpinner();
        NumMaquinasEmpaquetado = new javax.swing.JSpinner();

        setClosable(true);

        btnGuardarMaquinas.setText("Guardar");
        btnGuardarMaquinas.setToolTipText("");
        btnGuardarMaquinas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarMaquinasActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Horneado: ");

        jLabel2.setText("Decorado:");

        jLabel3.setText("Rellenado:");

        jLabel4.setText("Empaquetado:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(NumMaquinasEmpaquetado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(4, 4, 4)))
                        .addGap(79, 79, 79)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(NumMaquinasHorneado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NumMaquinasDecorado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NumMaquinasRellenado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(btnGuardarMaquinas)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NumMaquinasHorneado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(NumMaquinasDecorado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(NumMaquinasRellenado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(NumMaquinasEmpaquetado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardarMaquinas)
                    .addComponent(jButton2))
                .addGap(0, 22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarMaquinasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarMaquinasActionPerformed
           int numADesHabHorneado = 0 , numADesHabDecorado = 0, numADesHabRellenado = 0, numADesHabEmpaquetado =0 ;
        
            numADesHabHorneado = numHorneado - (int) NumMaquinasHorneado.getValue();
            numADesHabDecorado = numDecorado - (int) NumMaquinasDecorado.getValue();
            numADesHabRellenado =  numRellenado - (int) NumMaquinasRellenado.getValue();
            numADesHabEmpaquetado = numEmpaquetado - (int) NumMaquinasEmpaquetado.getValue();
            try{
            Controlador.Service.Produccion.GestionProduccion.DesabilitarMaquinas(maquinas,numADesHabHorneado,numADesHabDecorado,numADesHabRellenado,numADesHabEmpaquetado); 
            JOptionPane.showMessageDialog(this,"La operacion se realizó con éxito");
            ventanaAsignacionHM.ActualizarMaquinas();
            this.dispose();
            }catch(Exception e){
                JOptionPane.showMessageDialog(this,"Error al conectarse a la base de datos, intentelo mas tarde por favor");
            }
    }//GEN-LAST:event_btnGuardarMaquinasActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner NumMaquinasDecorado;
    private javax.swing.JSpinner NumMaquinasEmpaquetado;
    private javax.swing.JSpinner NumMaquinasHorneado;
    private javax.swing.JSpinner NumMaquinasRellenado;
    private javax.swing.JButton btnGuardarMaquinas;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

    private void initComp(ArrayList<Maquina> maquinasHabilidatas, ArrayList<Maquina> maquinasSeleccionables) {
        
        int numHorneadoHab = 0, numDecoradoHab =0, numRellenadoHab = 0, numEmpaquetadoHab = 0;
        int numHorneadoSel = 0, numDecoradoSel = 0, numRellenadoSel = 0, numEmpaquetadoSel = 0;
        for(int i = 0; i < maquinasHabilidatas.size(); i++){
            
            Maquina maquina = maquinasHabilidatas.get(i);
            switch (maquina.getIdTipoMaquina()) {
                case "TMA000001":
                    numHorneadoHab++;
                    break;
                case "TMA000002":
                    numRellenadoHab++;
                    break;
                case "TMA000003":
                    numDecoradoHab++;
                    break;
                case "TMA000004":
                    numEmpaquetadoHab++;
                    break;
            }           
        }
      
        
//                for(int i = 0; i < maquinasHabilidatas.size(); i++){
//            
//            Maquina maquina = maquinasSeleccionables.get(i);
//            switch (maquina.getIdTipoMaquina()) {
//                case "TMA000001":
//                    numHorneadoSel++;
//                    break;
//                case "TMA000002":
//                    numRellenadoSel++;
//                    break;
//                case "TMA000003":
//                    numDecoradoSel++;
//                    break;
//                case "TMA000004":
//                    numEmpaquetadoSel++;
//                    break;
//            }           
//        }
                this.numHorneado = numHorneadoHab;
                this.numDecorado = numDecoradoHab;
                this.numRellenado = numRellenadoHab;
                this.numEmpaquetado = numEmpaquetadoHab;
      NumMaquinasHorneado.setModel(new SpinnerNumberModel(numHorneadoHab,0,numHorneadoHab,1));
      NumMaquinasDecorado.setModel(new SpinnerNumberModel(numDecoradoHab,0,numDecoradoHab,1));
      NumMaquinasEmpaquetado.setModel(new SpinnerNumberModel(numEmpaquetadoHab,0,numEmpaquetadoHab,1));
      NumMaquinasRellenado.setModel(new SpinnerNumberModel(numRellenadoHab,0,numRellenadoHab,1));
        
    }
}
